package io.babystar.modules.business.controller;

import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.R;
import io.babystar.modules.business.entity.CkCategoryEntity;
import io.babystar.modules.business.service.CkCategoryService;
import io.babystar.modules.sys.entity.SysMenuEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 仓库-产品分类表（二级分类）
 *
 * @author rsq
 * @email rsq15234313042@163.com
 * @date 2021-03-03 10:42:29
 */
@RestController
@RequestMapping("business/ckcategory")
public class CkCategoryController {
    @Autowired
    private CkCategoryService ckCategoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:ckcategory:list")
    public List<CkCategoryEntity> list(@RequestParam Map<String, Object> params){
        List<CkCategoryEntity> ckCategoryEntityList = ckCategoryService.list();
        for(CkCategoryEntity ckCategoryEntity : ckCategoryEntityList){
            CkCategoryEntity parentCategoryEntity = ckCategoryService.getById(ckCategoryEntity.getPid());
            if(parentCategoryEntity != null){
                ckCategoryEntity.setParentCategory(parentCategoryEntity.getCategory());
            }
        }
        return ckCategoryEntityList;
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:ckcategory:info")
    public R info(@PathVariable("id") Integer id){
		CkCategoryEntity ckCategory = ckCategoryService.getById(id);

        return R.ok().put("ckCategory", ckCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:ckcategory:save")
    public R save(@RequestBody CkCategoryEntity ckCategory){
        Map<String,Object> map = new HashMap<>();
        map.put("category",ckCategory.getCategory());
        if(ckCategoryService.listByMap(map).size() == 0){
            ckCategoryService.save(ckCategory);
            return R.ok();
        }
        return R.error(1,"已存在该分类");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:ckcategory:update")
    public R update(@RequestBody CkCategoryEntity ckCategory){
		ckCategoryService.updateById(ckCategory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:ckcategory:delete")
    public R delete(@RequestBody Integer[] ids){
		ckCategoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
